/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConnectionBox.java
 *
 * Created on 23 févr. 2010, 11:35:47
 */
package chat.client;

import org.jdesktop.application.Action;

/**
 *  The application's connection box.
 * @author berthise
 */
public class ConnectionBox extends javax.swing.JDialog {

    /* Creates new form ConnectionBox */
    public ConnectionBox(ChatView client, java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.client = client;
        getRootPane().setDefaultButton(connectionButton);
        numPortSpinner.setValue(10009);
    }
    private ChatView client = null;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        infoLabel = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        nickNameLabel = new javax.swing.JLabel();
        nickNameTextField = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        serverLabel = new javax.swing.JLabel();
        serverTextField = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        numPortLabel = new javax.swing.JLabel();
        numPortSpinner = new javax.swing.JSpinner();
        jPanel5 = new javax.swing.JPanel();
        connectionButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setIconImage(null);
        setModal(true);
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(8, 8, 8, 8));
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(new java.awt.GridLayout(5, 0, 0, 10));

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        infoLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(ConnectionBox.class);
        infoLabel.setText(resourceMap.getString("infoLabel.text")); // NOI18N
        infoLabel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        infoLabel.setName("infoLabel"); // NOI18N
        jPanel2.add(infoLabel);

        jPanel1.add(jPanel2);

        jPanel3.setName("jPanel3"); // NOI18N
        jPanel3.setLayout(new java.awt.GridLayout(1, 2, 16, 0));

        nickNameLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        nickNameLabel.setText(resourceMap.getString("nickNameLabel.text")); // NOI18N
        nickNameLabel.setName("nickNameLabel"); // NOI18N
        jPanel3.add(nickNameLabel);

        nickNameTextField.setText(resourceMap.getString("nickNameTextField.text")); // NOI18N
        nickNameTextField.setName("nickNameTextField"); // NOI18N
        jPanel3.add(nickNameTextField);

        jPanel1.add(jPanel3);

        jPanel4.setName("jPanel4"); // NOI18N
        jPanel4.setLayout(new java.awt.GridLayout(1, 2, 16, 0));

        serverLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        serverLabel.setText(resourceMap.getString("serverLabel.text")); // NOI18N
        serverLabel.setName("serverLabel"); // NOI18N
        jPanel4.add(serverLabel);

        serverTextField.setText(resourceMap.getString("serverTextField.text")); // NOI18N
        serverTextField.setName("serverTextField"); // NOI18N
        jPanel4.add(serverTextField);

        jPanel1.add(jPanel4);

        jPanel6.setName("jPanel6"); // NOI18N
        jPanel6.setLayout(new java.awt.GridLayout(1, 2, 16, 0));

        numPortLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        numPortLabel.setText(resourceMap.getString("numPortLabel.text")); // NOI18N
        numPortLabel.setName("numPortLabel"); // NOI18N
        jPanel6.add(numPortLabel);

        numPortSpinner.setName("numPortSpinner"); // NOI18N
        jPanel6.add(numPortSpinner);

        jPanel1.add(jPanel6);

        jPanel5.setName("jPanel5"); // NOI18N
        jPanel5.setLayout(new java.awt.GridLayout(1, 2, 16, 16));

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(ConnectionBox.class, this);
        connectionButton.setAction(actionMap.get("setConnection")); // NOI18N
        connectionButton.setName("connectionButton"); // NOI18N
        jPanel5.add(connectionButton);

        cancelButton.setAction(actionMap.get("closeConnectionBox")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        jPanel5.add(cancelButton);

        jPanel1.add(jPanel5);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void closeConnectionBox() {
        dispose();
    }

    @Action
    public void setConnection() {
        String login = new String(nickNameTextField.getText());
        String serveur = new String(serverTextField.getText());
        int numPort = Integer.parseInt(numPortSpinner.getValue().toString());
        if (!login.isEmpty() && !serveur.isEmpty()) {
            client.initCommunication(login, serveur, numPort);
            dispose();
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton connectionButton;
    private javax.swing.JLabel infoLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JLabel nickNameLabel;
    private javax.swing.JTextField nickNameTextField;
    private javax.swing.JLabel numPortLabel;
    private javax.swing.JSpinner numPortSpinner;
    private javax.swing.JLabel serverLabel;
    private javax.swing.JTextField serverTextField;
    // End of variables declaration//GEN-END:variables
}
